package com.xh.controller.data;

import com.xh.service.BlogService;
import com.xh.service.CommentService;
import com.xh.service.PraiseService;
import com.xh.service.UserService;
import com.xh.utils.MD5Location;
import com.xh.vo.Blog;

import com.xh.vo.Comment;
import com.xh.vo.LogUser;
import com.xh.vo.Praise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;


@Controller
public class FormController {
    @Resource
    private BlogService blogService;
    @Resource
    private UserService userService;
    @Resource
    private PraiseService praiseService;
    @Resource
    private CommentService commentService;

    @PostMapping("/newBlog")
    public String newBlog(Blog blog) {
        blogService.NewBlog(blog);
        return "redirect:blog/index";
    }

    @GetMapping("/toBlogContent/{Bid}/{userId}")
    public String toXqContent(@PathVariable("Bid") Integer bId, @PathVariable("userId") String userId, Model model) {

        int i = Integer.parseInt(userId);

        Blog blog = blogService.getBlogListById(bId);
        model.addAttribute("blog", blog);
        List<Praise> praise = praiseService.queryPraise(i, bId);
        if (praise.isEmpty()) {
            model.addAttribute("isPraise", false);
        } else {
            model.addAttribute("isPraise", true);
        }
        //根据文章的 id 查询文章所对应的评论
        Comment comment = new Comment(bId);
        List<Comment> commentList = commentService.queryComment(comment);
        model.addAttribute("commentList", commentList);
        return "blog/blogContent";
    }

    @GetMapping("/toRegister")
    public String toRegister() {
        return "blog/register";
    }

    @PostMapping("/register")
    public String register(@RequestParam("userName") String userName,
                           @RequestParam("pwd") String pwd,
                           @RequestParam("phone") String phone,
                           @RequestParam("birthday") String birthday,
                           @RequestParam("email") String email, Model model) {
        String md5Pwd = MD5Location.getMd5(pwd);
        LogUser logUser = new LogUser(userName, md5Pwd, email, phone, birthday);
        userService.InsertLoginUser(logUser);
        model.addAttribute("errMsg", "注册成功，请登录!");
        return "blog/login";
    }

    @PostMapping("updatePwd")
    public String updatePwd(@RequestParam("newPwd") String Pwd,
                            @RequestParam("userId") Integer userId,
                            @RequestParam("orgPwd") String orgPwd, HttpSession session, Model model) {
        String md5 = MD5Location.getMd5(Pwd);
        String md51 = MD5Location.getMd5(orgPwd);

        LogUser logUser = userService.queryPwd(userId, md51);
        if (logUser != null) {
            userService.updatePwdByUserId(userId, md5);
            session.invalidate();
            model.addAttribute("errMsg", "修改成功，请登录!");
            return "blog/login";
        } else {
            model.addAttribute("errMsg", "原密码错误！");
            return "blog/updatePwd";
        }

    }
}
